function openEditPage(url,width, height,name,level){

	var width = width || 420;
	var height = height || 280;
	dialogOpen(url,width,height,name,level);
}
function wrongAlert(message, width, height){
	width = width || 380;
	height = height || 185;
//	message = encodeURI(message);
	dialogOpen("pages/webframework/common/wrong.jsp?message=" + message ,380,185,"");
}

function dialogOpen(url,width,height,name,level){
//	alert(url);
//	alert(level);
	var xposition = (screen.width - width) / 2;
	var yposition = (screen.height - height-25) / 2;
	var iframeId = 'iframeId_'+new Date().getTime();
	var windowId = 'windowId_'+new Date().getTime() ;
	url = encodeURI(url);
	top.windowOpen(url, windowId, name, height, width, true, iframeId, window);
	//var win = window.showModalDialog(url+"&time="+new Date().getTime(),window,'dialogWidth:'+width+'px;dialogHeight:'+height+'px;help:no;status:no;scroll:no;center');
//	var win = window.open(url+"&time="+new Date().getTime(),'newwindow','height='+height+', width='+width+', toolbar =no, menubar=no, scrollbars=no, resizable=no, location=no, status=no,left='+xposition+',top='+yposition) ;
}

function trim(Str , Flag){
	Str	= ""+Str;
	if( Flag == "l" || Flag == "L" )/*trim left side only*/
	{
		RegularExp	= /^\s+/gi;
		return Str.replace( RegularExp,"" );
	}
else if( Flag == "r" || Flag == "R" )/*trim right side only*/
	{
		RegularExp	= /\s+$/gi;
		return Str.replace( RegularExp,"" );
	}
else/*defautly, trim both left and right side*/
	{
		RegularExp	= /^\s+|\s+$/gi;
		return Str.replace( RegularExp,"" );
	}
}

function checkpage(totalPages){
	//	alert('checkpage');
	var frm=document.getElementById('textNum').value;
	if( document.getElementById('ec_eti')!=null)
	document.getElementById('ec_eti').value="";

	var checknumber=/^[0-9]+$/;

	if(frm!="" && trim(frm)!="0"){

		if(!checknumber.test(trim(frm))){
			alert("\u8bf7\u8f93\u5165\u6b63\u786e\u7684\u9875\u6570\uff01");
			document.location.reload()  ;
		}else {

			var maxpage=eval(totalPages);
			var inputNum=eval(frm);

			if(inputNum>maxpage){

				document.forms.ec.setAttribute('action',document.location.href);
				document.forms.ec.setAttribute('method','post');
				document.forms.ec.ec_p.value=maxpage;
				document.forms.ec.submit();
			}else{
				document.forms.ec.setAttribute('action',document.location.href);
				document.forms.ec.setAttribute('method','post');
				document.forms.ec.ec_p.value=trim(frm);
				document.forms.ec.submit();
			}
		}
	}else{
		alert("\u9875\u6570\u4e3a\u7a7a\uff0c\u8bf7\u8f93\u5165\u6b63\u786e\u7684\u9875\u6570!");
		document.location.reload();
	}
}

function formSubmit(id){
	var form = document.getElementById(id);
	form.submit();


}

function buttonSubmit(id){
//	alert(id);
	var submitButton = document.getElementById(id);
	submitButton.click();

}

function onKeyUpSubmit(id)
{

	if (event.keyCode == 13)
	{
		formSubmit(id);

	}
}

function buttonOpenEditPage(url,width, height,name,level,primaryKey){
	itemId = getSelectIds(true);
	if(!itemId){
		dialogOpen("/res/pages/webframework/common/wrong.jsp?message=\u8bf7\u5728\u5217\u8868\u4e2d\u9009\u62e9\u8981\u4fee\u6539\u7684\u8bb0\u5f55!",380,185,"错 误",level);

		return false;
	}
	if(!primaryKey) primaryKey ="id";

	url = url +  "&"+primaryKey+"=" + itemId;
	openEditPage(url,width, height,name,level);
}

function getAllSelectIds(tableId){
	var ids="";
	var listTable = null;
	if(tableId != undefined){
		listTable =  document.getElementById(tableId);
	}else{
		listTable =  document.getElementById('listTable');
	}

	var objs = listTable.getElementsByTagName("tr");

	for (var i = 0; i < objs.length; i++) {
		//	objs[i].style.backgroundColor = "#ff88ff";
		if(objs[i].id != null && objs[i].id != ''){
			ids += objs[i].id;

			if(i !=  objs.length){
				ids += ';';
			}
		}
	}
	return ids;

}

function getSelectIds(selectFirstId,tableId){
	var ids="";
	var listTable = null;
	if(tableId != undefined){
		listTable =  document.getElementById(tableId);
	}else{
		listTable =  document.getElementById('listTable');
	}

	//	alert(listTable);
	var selected = false;
	var objs = listTable.getElementsByTagName("tr");

	for (var i = 0; i < objs.length; i++) {
		//	objs[i].style.backgroundColor = "#ff88ff";
		if(objs[i].className == 'gridSelectOn'){
			ids += objs[i].id;
			selected = true;
			if(i !=  objs.length){
				ids += ';';
			}
		}

	}
	if(selectFirstId != undefined){
		if(ids=="") return false;
		return ids.substr(0,ids.indexOf(";"));
	}else{
		return ids;
	}

}


function goDelete(actionStr, actionMethod, level){
	var ids=getSelectIds();

	if(actionStr == undefined){
		actionStr = getActionStr(window.location.href);
		if(actionMethod != undefined){
			actionStr = actionStr + "?method=" +actionMethod;
		}else{
		    actionStr += "?method=delete";
		}
	}

	if(ids==""){
		dialogOpen("/res/pages/webframework/common/wrong.jsp?message=\u8bf7\u9009\u62e9\u8981\u5220\u9664\u7684\u8bb0\u5f55!",380,185,"错 误",level);
		return;
	}
	var message = "\u786e\u5b9a\u8981\u5220\u9664\u6240\u9009\u8bb0\u5f55\u5417?";
	dialogOpen("/res/pages/webframework/common/enquiry.jsp?message="+message,380,185,"询 问",level);
	window.actionStr = actionStr;

}

function confirmCallBack(){

	var ids=getSelectIds();
	if(ids != null && ids != '' && window.actionStr != undefined){
		document.getElementById("primaryIDs").value=ids;
		document.listForm.action = actionStr;
		document.listForm.submit();
	}

}

function getActionStr(str){
	var re = /\w+\.do?/g;
	var arr = re.exec(str);
	return arr;
}

function setQueryFlag(flag){
	var queryFlag = document.getElementById('queryFlag');
	queryFlag.value = flag;
}

function changeIframeUrl(iframeId, url){

//	alert(iframeId)

	var iframe = document.getElementById(iframeId);
//alert(url)
	if(url == null || url == "null"){

		url = iframe.src + "&time="+new Date().getTime();
	//	alert("url"+url);
	//	alert("iframe.src"+iframe.src);
	}

	//alert(url);
	if(iframe != null)
		iframe.src = url;
}

function iframeSubmit(formId, iframeId, url){
	url += "&time="+new Date().getTime();
	var form =  document.getElementById(formId);
//	alert(form);

	var objs = form.getElementsByTagName("input");
	url = generateUrlWithParas(url,objs);
	objs = form.getElementsByTagName("select");
	url = generateUrlWithParas(url,objs);
	objs = form.getElementsByTagName("textarea");
	url = generateUrlWithParas(url,objs);
	url = encodeURI(url);
//		alert(url);
	changeIframeUrl(iframeId, url);
}


function generateUrlWithParas(url, paras){
//	alert(paras.length);
	for(var i=0;i<paras.length;i++){
		var value = '';

		if(paras[i].type == "checkbox"){
			if(paras[i].checked == true)
				value = paras[i].value;

		}else{
			value = paras[i].value;
		}
		if(value != null && value != '')
			url += "&" + paras[i].name + "=" + value;

	}
	return url;
}

function getValueById(id){
	var obj = document.getElementById(id);
	if(obj != null){
		return obj.value;
	}
}
function getParentValueById(id){

	var obj = window.parent.document.getElementById(id);
	if(obj != null){
		return obj.value;
	}
}

function handleKeyEvents(){
	if(event.keyCode == 46){
		var activeObj = document.activeElement;
		if(activeObj != undefined && activeObj != null){
			if(activeObj.type != "text"){//判断type不是text执行del
				var obj = document.getElementById("delete");
				if(obj != null){
					obj.click();
				}
			}
		}
	}
}
function keyUp(){

	handleKeyEvents();

}

document.onkeyup=keyUp;

function buttonDeletePage(url,width, height){
	itemId = getSelectIds(true);
	if(!itemId){
		alert("请选择要删除的记录!");
		return false;
	}
	window.showModalDialog
	        ('/res/pages/trade/Aux_warning.htm',window,'dialogWidth:330px;dialogHeight:170px;help:no;status:no;scroll:no;certer')
}